package com.gccloud.design;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gccloud.design.entity.StfBusinessTypeEntity;
import com.gccloud.design.feign.BwpOperDockingClient;
import com.gccloud.design.feign.DictClient;
import com.gccloud.design.service.IStfBusinessTypeService;
import com.gccloud.starter.common.exception.GlobalException;
import com.gccloud.starter.common.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName : StfBusinessTypeController
 * @Author :
 * @Date : 2020/12/15 14:49
 * @Description :
 */

@RestController
@Api(tags = "业务类型管理")
@RequestMapping("/businessType")
public class StfBusinessTypeController {

    @Autowired
    private IStfBusinessTypeService stfBusinessTypeService;

    @Resource
    private DictClient dictClient;

    @Resource
    private BwpOperDockingClient bwpOperDockingClient;

    /**
     * 新增业务类型
     *
     * @param stfBusinessTypeEntity
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增业务类型", notes = "新增业务类型", produces = MediaType.APPLICATION_JSON_VALUE)
    public R add(@ApiParam(name = "新增业务类型", value = "传入json格式", required = true) @RequestBody StfBusinessTypeEntity stfBusinessTypeEntity) {
        return stfBusinessTypeService.add(stfBusinessTypeEntity);
    }

    /**
     * 修改业务类型
     *
     * @param stfBusinessTypeEntity
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改业务类型", notes = "修改业务类型", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<Void> update(@ApiParam(name = "修改业务类型", value = "传入json格式", required = true) @RequestBody StfBusinessTypeEntity stfBusinessTypeEntity) {
        stfBusinessTypeService.update(stfBusinessTypeEntity);
        return R.success();
    }

    /**
     * 根据id删除业务类型
     *
     * @param idStr
     * @return
     */
    @PostMapping("/deleteById/{id}")
    @ApiOperation(value = "根据id删除业务类型", notes = "删除业务类型", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<Void> deleteById(@ApiParam(name = "删除业务类型", value = "传入String格式", required = true) @PathVariable("id") String idStr) {
        List<String> ids = convert(idStr);
        for (String id : ids) {
            StfBusinessTypeEntity stf=stfBusinessTypeService.getBaseDao().selectById(id);
            stfBusinessTypeService.getBaseDao().deleteByIdWithDp(id);
//            dictClient.deleteByValue(stf.getCode());
            // 同步删除流程平台的数据
            bwpOperDockingClient.deleteBwpBusiType(stf.getTenantId());
        }
        return R.success();
    }

    @PostMapping("/deleteByCode/{codes}")
    @ApiOperation(value = "根据编码删除业务类型", notes = "删除业务类型", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<Void> deleteByCode(@ApiParam(name = "codes", value = "传入String[]格式", required = true) @PathVariable("codes") String[] codes) {
        for (String code : codes) {
            QueryWrapper<StfBusinessTypeEntity> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("CODE", code);
            stfBusinessTypeService.getBaseDao().deleteWithDp(queryWrapper);
            dictClient.deleteByValue(code);
        }
        return R.success();
    }

    /**
     * 根据businessTypeId获取amid
     *
     * @param businessTypeId
     * @return
     */
    @ApiOperation(value = "查询amId", notes = "根据businessTypeId获取amid", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping("/getAmidBybusinessTypeId/{businessTypeId}")
    public R<Integer> getAmidBybusinessTypeId(@ApiParam(name = "businessTypeId", value = "传入String格式", required = true) @PathVariable("businessTypeId") String businessTypeId) {
        StfBusinessTypeEntity businessType = stfBusinessTypeService.getBaseDao().selectByIdWithDp(businessTypeId);
        if(businessType == null){
            return R.success(null);
        }
        return R.success(businessType.getAmId());
    }

    /**
     * 根据amId获取businessTypeId
     *
     * @param amId
     * @return
     */
    @ApiOperation(value = "查询", notes = "根据amId获取businessTypeId", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping("/getBusinessTypeByamId/{amId}")
    public R<List<String>> getBusinessTypeByamId(@ApiParam(name = "amId", value = "传入String格式", required = true) @PathVariable("amId") String amId) {
        List<String> businessTypeIds = stfBusinessTypeService.getBusinessTypeByamId(amId);
        return R.success(businessTypeIds);
    }

    public List<String> convert(String idStr) {
        if (StringUtils.isBlank(idStr)) {
            throw new GlobalException("id不允许为空");
        } else {
            String[] idStrArr = StringUtils.split(idStr, "-");
            return (List) Arrays.stream(idStrArr).map((id) -> {
                return id;
            }).collect(Collectors.toList());
        }
    }


}
